package XML;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;

public class DeleteXMLDataDemo {


    static File file = new File("D:\\GitHOME\\information\\text.xml");

    public static void move(int k) {

       // public static void main(String[] args) throws Exception {

        DocumentBuilder newDocumentBuilder = null;
        try {
            newDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        Document doc = null;
        try {
            doc = newDocumentBuilder.parse(file);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Element root = doc.getDocumentElement();


        //查找指定节点

        //通过根节点获得子节点

        NodeList personList = root.getElementsByTagName("spot");

//        int i= Integer.parseInt();
     /*  System.out.println("请输入一个整数：");
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();*/
        Node item = personList.item(k);

        //移出节点

        root.removeChild(item);


        Transformer transformer = null;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }

        Source source = new DOMSource(doc);

        Result result = new StreamResult(file);

        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }


}


